package com.cc.shiro.demo.realm;

import com.cc.shiro.demo.entity.User;
import com.cc.shiro.demo.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.Set;

/**
 * @Description: 用一句话描述
 * @Auther: 新梦想*陈超
 * @Date: 2019-12-16 09:34
 */
@Slf4j
public class UserRealm extends AuthorizingRealm {

    @Autowired
    private UserMapper userMapper;

    /*授权*/
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //1.身份
        String userName = (String) principalCollection.getPrimaryPrincipal();
        //2.身份获取角色和权限
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //根据身份从数据库获取该身份拥有的角色或权限
        //假设zhang这个用户拥有admin角色，和user:create  user:update menu:create
        Set<String> roles = new HashSet<>();
        //方式一：根据角色来授权
//        if(userName.equals("zhang")){
//            roles.add("admin");
//            authorizationInfo.setRoles(roles);
//        }else{
//            roles.add("guest");
//            authorizationInfo.setRoles(roles);
//        }
        //方式二：根据权限
        Set<String> permitions = new HashSet();
        if(userName.equals("zhang")){
            permitions.add("user:create");
            permitions.add("user:update");
            permitions.add("menu:create");
        }else{
            permitions.add("user:create");

        }
        authorizationInfo.setStringPermissions(permitions);

        //log.info("【授权信息为：{}】",authorizationInfo.getRoles().iterator().next());
        log.info("【授权信息为：{}】",authorizationInfo.getStringPermissions().iterator().next());
        return authorizationInfo;

    }
    /*身份认证*/
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
       //两个关键：身份   +  证明或凭证
        //1.身份(登录的用户名)
        String principal = (String) authenticationToken.getPrincipal();//身份(用户名)
        //2.凭证（数据库）
        User user = userMapper.findUserByUserName(principal);
        log.info("[登录的查询用户信息为：{}]",user);
        /*
           要将用户的凭证返回给shiro:用户名，用户加密后的密码，盐，shiro将其缓存起来。

           下次再登录的时候，shiro根据用户名，明文密码到shiro缓存中匹配用户凭证


           md5加密算法
            普通md5加密： 密码(123456)--->md5算法---->密文的密码（Af09dfasdf333d）
            md5加密+加盐：密码(123456)+盐(数据库)--->md5算法2次加密---->密文的密码（Af09dfasdf333d）
         */
        SimpleAuthenticationInfo authenticationInfo = new 	SimpleAuthenticationInfo(principal,
                user.getPassword(), 	//密文
                ByteSource.Util.bytes(user.getPasswordSalt()), //盐
                getName());

        return authenticationInfo;
    }
}
